home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-15 | 3.1 KB | 120 lines | [ftFC/NLft] |
- {
- This program calculates the electric field of two opposite
- charges arranged at points -1/0 and 1/0.
- To run this program, first add it to pro Fit by choosing
- "Add to Menu" from the "Misc" menu. Then open a new
- drawing window. Then select the program ("DipoleField")
- from the "Run Program" submenu in the "Calc" menu.
-
- This program can be adapted to plotting other vector fields by
- modifying the procedure "GetField" only.
- }
-
- program DipoleField;
-
- const
- arrowAngle = 30; { arrow opening angle }
- arrowLen = 0.02; { arrow length }
- rangeX = 2;
- rangeY = 1;
- zoom = 0.02; { zoom factor for drawing vectors }
- maxArrow = 0.10; { the max length of an arrow }
-
- var Ex, Ey: real; { the results of ElectricField }
- E1x, E1y: real;
-
- x,y: real;
- VectorX, VectorY:real; {set by the function GetField }
- sinA, cosA: real; { sine and cos of arrow angle times arrow length }
-
-
-
- procedure DrawArrow(x,y, vx,vy);
- { draws an arrow at position x,y with vector vx, vy }
- var tipX, tipY: real;
- ax, ay, r: real;
- begin
- vx := vx*zoom; vy := vy*zoom;
- r := sqrt(sqr(vx) + sqr(vy));
- if (r < maxArrow) and (r > 1e-30) then
- begin
- tipX := x + vx/2;
- tipY := y + vy/2;
- MoveTo(x-vx/2, y-vy/2);
- LineTo(tipX, tipY);
- if r >= arrowLen then { show arrow only if shaft long enough }
- begin
- vx := vx/r; vy := vy/r;
- ax := cosA*vx - sinA*vy;
- ay := sinA*vx + cosA*vy;
- MoveTo(tipX-ax, tipY-ay);
- LineTo(tipX, tipY);
- ax := cosA*vx + sinA*vy;
- ay := -sinA*vx + cosA*vy;
- LineTo(tipX-ax, tipY-ay);
- end;
- end;
- end; { DrawArrow }
-
-
- procedure ElectricField(x,y, xx0, yy0, c);
- { returns the field of a single charge }
- var r3;
- begin
- x := x-xx0;
- y := y-yy0;
- r3 := (sqr(x) + sqr(y)) ^1.5;
- if (r3 < 1e-30) then
- begin
- Ex := 0; Ey := 0;
- end
- else
- begin
- Ex := c * x/r3; Ey := c * y/r3;
- end;
- end;
-
- {*************************************************************************}
- { this function calculates the vector field to be plotted. }
- { substitute it with a function of yours if you want to plot }
- { other vector rields. Presently it calls the procedure }
- { ElectricField to calculate the field of a dipole. }
-
- procedure GetField(x,y:real);
- begin
- ElectricField(x,y, 1, 0, 1);
- E1x := Ex; E1y := Ey;
- ElectricField(x,y, -1, 0, -1);
-
- VectorX:= Ex+E1x;
- VectorY:= Ey+E1y
- end;
- {*************************************************************************}
-
-
- begin
- sinA := sin(arrowAngle*π/180) * arrowLen;
- cosA := cos(arrowAngle*π/180) * arrowLen;
- SetNewGraphRect(30, 30, 430, 230);
- CreateNewGraf(-rangeX,rangeX,-rangeY,rangeY,0,0);
- SetLineStyle(0.5, 1);
- OpenCurve('electric field');
- x := -rangeX;
- while x <= rangeX do
- begin
- y := -rangeY;
- while y <= rangeY do
- begin
- GetField(x,y); {This function calculates the vector from the}
- {coordinate x,y and sets VectorX,VectorY}
- DrawArrow(x,y, VectorX,VectorY);
- y := y+rangeY/10;
- end; {while}
- x := x+rangeY/10;
- end; {while}
- CloseCurve;
- SetLineStyle(1, 1); {reset line style}
- end;
-
-
-